<template>
  <!-- vue3页⾯ -->
  <div class="home-page container-md">
    <section class=" text-center container">
      <div class="row py-lg-5">
        <div class="col-lg-6,col-md-8 mx-auto">
          <img src="../assets/callout.svg"
               alt="callout"
               class="w-25">
          <h2 class="font-weight-light">随心写作,自由表达</h2>
          <p>
            <a href="#"
               class="btn btn-primary my-2"
               @click.prevent="createOrLogin">开始写作</a>
          </p>
        </div>
      </div>
    </section>
    <h4 class="font-weight-bold text-center mb-4">发现精彩</h4>
    <ColumnList :list="list"></ColumnList>
    <button class="btn btn-outline-primary mt-2 mb-5 mx-auto btn-block w-25 d-block"
            @click="loadMorePage"
            v-if="!isLastPage">
      加载更多
    </button>
  </div>
</template>

<script lang='ts'>
import { defineComponent, computed, onMounted } from 'vue'
import { useStore } from 'vuex'
import { GlobalDataProps } from '@/store'
import ColumnList from '../components/ColumnList.vue'
import useLoadMore from '@/hook/useLoadMore'
import router from '@/router'

export default defineComponent({
  name: 'Home',
  components: { ColumnList },
  setup() {
    const store = useStore<GlobalDataProps>()
    const total = computed(() => store.state.columns.total)
    const currentPage = computed(() => store.state.columns.currentPage)
    onMounted(() => {
      store.dispatch('fetchColumns')
    })
    const list = computed(() => store.getters.getColumns)
    const { loadMorePage, isLastPage } = useLoadMore('fetchColumns', total,
      { pageSize: 3, currentPage: (currentPage.value ? currentPage.value + 1 : 3) })

    const createOrLogin = () => {
      const isLogin = store.state.user.isLogin
      if (!isLogin) {
        router.push('/login')
      } else {
        router.push('/create')
      }
    }

    return {
      list,
      loadMorePage,
      isLastPage,
      createOrLogin
    }
  }
})
</script>

<style scoped>
</style>